<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.File"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";

	// 验证上传内容了类型
	String contentType = request.getContentType();
	String fullFilePath="";
	
	if (contentType!=null&&contentType.indexOf("multipart/form-data") >= 0) {
		int maxFileSize = 2 * 1024 * 1024;
		int maxMemSize = 2 * 1024 * 1024;
		ServletContext context = pageContext.getServletContext();
		String filePath = context.getRealPath("/userIcons");

		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 设置内存中存储文件的最大值
		factory.setSizeThreshold(maxMemSize);
		// 本地存储的数据大于 maxMemSize.
		factory.setRepository(new File("e:\\temp"));

		// 创建一个新的文件上传处理程序
		ServletFileUpload upload = new ServletFileUpload(factory);
		// 设置最大上传的文件大小
		upload.setSizeMax(maxFileSize);
		try {
			// 解析获取的文件
			List<FileItem> fileItems = upload.parseRequest(request);

			// 处理上传的文件
			Iterator<FileItem> i = fileItems.iterator();
			while (i.hasNext()) {
				FileItem fi = (FileItem) i.next();
				if (!fi.isFormField()) {
					// 获取上传文件的参数
					String fileName = fi.getName();
					Date date = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
					String now = sdf.format(date);
					File file = new File(filePath, now + ".jpg");
					fi.write(file);
					fullFilePath= basePath+"userIcons/"+file.getName();
				}
			}
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>UploadImg</title>
<script type="text/javascript" src="<%=basePath %>js/jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
	margin: 0px;
	padding: 0px;
	font-family: Arial;
	font-size: 12px;
}

p {
	margin: 0px;
	padding: 4px 0px;
}

input {
	border: solid 1px #ddd;
}

input.btn {
	border-bottom-color: #999;
	border-right-color: #999;
}

#sp_err {
	color: #ff0000;
}
</style>
</head>
<body onload="upload()">
	<form action="" method="post" enctype="multipart/form-data"
		onsubmit="return chkForm();">
		<div style="padding: 20px;">
			<p>选择你要上传的图片文件</p>
			<p>
				<input id="file1" name="file1" type="file" filePath="<%=fullFilePath %>" style="width: 220px;" />
				<input id="btnUp" class="btn" type="submit" value="上 传" /> <input
					id="shuiyin" name="shuiyin" type="hidden" />
			</p>

			<p>
				1、图片大小不能超过<b>2M</b>
			</p>
			<p>2、支持格式：.jpg .gif .png .bmp</p>
			<!--             <p>3、如需管理图片，请进入<a href="http://my.csdn.net/my/album" target="_blank">个人空间相册</a></p> -->
			<p>
				<span id="sp_err"></span>
			</p>
		</div>
	</form>
	<script type="text/javascript">
		function GetShuiyin() {
			//得到父页面水印的样式
			if (window.parent.document.getElementById('isshuiyin1').checked) {
				document.getElementById('shuiyin').value = window.parent.document
						.getElementById('isshuiyin1').value;
			}
			if (window.parent.document.getElementById('isshuiyin2').checked) {
				document.getElementById('shuiyin').value = window.parent.document
						.getElementById('isshuiyin2').value;
			}
			if (window.parent.document.getElementById('isshuiyin3').checked) {
				document.getElementById('shuiyin').value = window.parent.document
						.getElementById('isshuiyin3').value;
			}

		}

		function setPath(path) {
			parent.document.getElementById("xheImgUrl").value = path;
			parent.xh.removeModal();
		}
		function showErr(err) {
			document.getElementById("sp_err").innerHTML = "* " + err;
		}
		function chkForm() {
			var f = document.getElementById("file1").value;
			if (!f) {
				showErr("请选择要上传的图片。");
				return false;
			}
			var ext = f.substr(f.lastIndexOf(".") + 1).toLowerCase();
			if (".jpg.gif.png.bmp.".indexOf('.' + ext + '.') == -1) {
				showErr("图片格式不正确。");
				return false;
			}
// 			GetShuiyin();
			document.getElementById('btnUp').value = '正在上传...';
			document.getElementById('btnUp').disabled = true;
			return true;
		}
	</script>
	<script type="text/javascript">
		function upload(){
			var fullFileName=$("#file1").attr("filePath");
				if(fullFileName.trim()==''){
// 					alert("null");
					return;
				}else{
// 					alert("not null");
					showErr('');
					setPath(fullFileName);
				}
		}
	</script>
</body>
</html>

